package com.sumapay.fsb.gensrc.controller;


import com.sumapay.fsb.common.util.CommonUtil;
import com.sumapay.fsb.gensrc.service.IFsbUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.ServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suma
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/user")
public class FsbUsersController {
    @RequestMapping("/register")
    public String register(ServletRequest servletRequest) {
        MultiValueMap<String, Object> requestMap = CommonUtil.processRequestParameter(servletRequest);
        String result = iFsbUsersService.register(requestMap);
        return result;
    }

    @RequestMapping("/login")
    public String login(ServletRequest servletRequest){
        Map<String,Object> requestMap = CommonUtil.paramConvertToSourceMap(servletRequest);
        return iFsbUsersService.login(requestMap);
    }

    @RequestMapping("/isVerified")
    public String isVerified(ServletRequest servletRequest){
        Map<String,Object> requestMap = CommonUtil.paramConvertToSourceMap(servletRequest);
        return iFsbUsersService.isVerified(requestMap);
    }

    @RequestMapping("/authenticate")
    public String authenticate(StandardMultipartHttpServletRequest servletRequest){
        Map<String,Object> requestMap = CommonUtil.paramConvertToSourceMap(servletRequest);
        List<MultipartFile> fileList = servletRequest.getMultiFileMap().get("file");
        try {
            requestMap.put("ID_PIC_FRONT",fileList.get(0).getBytes());
            requestMap.put("ID_PIC_REVERSE",fileList.get(1).getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return iFsbUsersService.authenticate(requestMap);
    }


    private IFsbUsersService iFsbUsersService;

    @Autowired
    public void setIFsbUsersService(IFsbUsersService iFsbUsersService) {
        this.iFsbUsersService = iFsbUsersService;
    }

}
